package ui.panel.setup.subsidiary;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Paint;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.util.Duration;
import ui.factory.SingletonFactory;

import java.io.IOException;

public class SetUpChoiceStage {
    private static SetUpChoiceStage instance = null;
    private AnchorPane anchorPane = new AnchorPane();

    //调用单例工厂
    public static SetUpChoiceStage getInstance() {
        if (instance == null) {
            instance = SingletonFactory.getWeakInstace(SetUpChoiceStage.class);
        }
        return instance;
    }

    public AnchorPane getPane() throws IOException {

        anchorPane.setStyle("-fx-background-color:TRANSPARENT;");
        anchorPane.setLayoutX(450);
        anchorPane.setLayoutY(100);
        one(anchorPane);
        two(anchorPane);
        Timeline threeTimeline = new Timeline();
//        timeline.setOnFinished(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent actionEvent) {
//
//            }
//        });
        three(anchorPane, threeTimeline);
        threeTimeline.play();
        Timeline fourTimeline = new Timeline();
        four(anchorPane, fourTimeline);
        fourTimeline.play();
        five(anchorPane);
        six(anchorPane);
        seven(anchorPane);
        logoText(anchorPane);

//        smilingFace(anchorPane);
//        badFace(anchorPane);
//        weirdFace(anchorPane);
//        favoriteTypeOfDailySelectionMenu();
        anchorPane.getChildren().add(SetUpFavoriteTypeMenu.getInstance().favoriteTypeOfComicsAndAnimation());


//延时加载
//        new Timer().schedule(new TimerTask() {
//            public void run() {
//                Platform.runLater(new Runnable() {
//                    public void run() {
//                        button1.setText("我的新名字是100");
//                    }
//                });
//            }
//        }, 2000);


//        RotateTransition rt = new RotateTransition(Duration.seconds(2), anchorPane);
//        rt.setFromAngle(0);
//        rt.setToAngle(360);
////        rt.setByAngle(90);  // 设置旋转的角度
//        rt.setCycleCount(Animation.INDEFINITE);  // 设置旋转次数，我们需要旋转无数次
//        rt.setInterpolator(Interpolator.LINEAR);  // 控制每个过渡周期的加速和减速时间，设置为匀速
//        rt.play();  // 开始播放动画
        return anchorPane;
    }

    private void logoText(AnchorPane root) throws IOException {
        Group textGroup = new Group();
        //Font class instance
        Font font = Font.loadFont(SetUpChoiceStage.class.getResource("/fxml/font/logo.otf").openStream(), 35);

        String logo = "ICEOLOGY";
        double rotation = 30;
        //Loop
        for (char c : logo.toCharArray()) {
            // ignore whitespace, otherwise add rotated char
            if (!Character.isWhitespace(c)) {
                Text text = new Text(325, 30, Character.toString(c));
                text.setFont(font);
                text.setFill(Paint.valueOf("#84C1FF"));

                Rotate rotationMatrix = new Rotate(rotation, 335, 285d);
                text.getTransforms().add(rotationMatrix);

                textGroup.getChildren().add(text);
            }
            rotation += 7.2;
        }

        root.getChildren().add(textGroup);
    }

    private void one(AnchorPane root) {
        Arc arc = newArc(300, 300, 300, 300, 55, 5);
        arc.setStrokeLineCap(StrokeLineCap.BUTT);
        arc.setStrokeWidth(50);

        Arc arc1 = newArc(300, 300, 290, 290, 75, 90);
        arc1.setStrokeLineCap(StrokeLineCap.BUTT);
        arc1.setStrokeWidth(25);
        arc1.getStrokeDashArray().addAll(0.0, 10.0, 10.0, 10.0);

        root.getChildren().addAll(arc, arc1);
    }

    private void two(AnchorPane root) {
        Arc arc1 = newArc(300, 300, 260, 260, 180, 0);
        arc1.setStrokeLineCap(StrokeLineCap.ROUND);
        arc1.getStrokeDashArray().addAll(0.0, 10.0, 10.0, 10.0);
        arc1.setStrokeWidth(6);

        Arc arc2 = newArc(300, 300, 260, 260, 182, 176);
        arc2.setStrokeLineCap(StrokeLineCap.ROUND);
        arc2.setStrokeWidth(16);


        root.getChildren().addAll(arc1, arc2);
    }

    private void three(AnchorPane root, Timeline timeline) {
        int radiusXY = 235;
        Arc arc = newArc(300, 300, radiusXY, radiusXY, 80, 0);
        arc.setStrokeLineCap(StrokeLineCap.BUTT);
        arc.setStrokeWidth(18);

        Arc arc1 = newArc(300, 300, radiusXY, radiusXY, 130, 80);
        arc1.setStrokeLineCap(StrokeLineCap.BUTT);
        arc1.setStrokeWidth(7);

        Arc arc2 = newArc(300, 300, radiusXY, radiusXY, 20, 210);
        arc2.setStrokeLineCap(StrokeLineCap.SQUARE);
        arc2.setStrokeWidth(18);

        Arc arc3 = newArc(300, 300, radiusXY, radiusXY, 30, 230);
        arc3.setStrokeLineCap(StrokeLineCap.BUTT);
        arc3.setStrokeWidth(7);

        Arc arc4 = newArc(300, 300, radiusXY, radiusXY, 45, 260);
        arc4.setStrokeLineCap(StrokeLineCap.BUTT);
        arc4.setStrokeWidth(18);

        Arc arc5 = newArc(300, 300, radiusXY, radiusXY, 55, 305);
        arc5.setStrokeLineCap(StrokeLineCap.BUTT);
        arc5.setStrokeWidth(7);


        Rotate rotate1 = new Rotate(0, 300, 300);
        arc.getTransforms().add(rotate1);
        KeyValue kv1 = new KeyValue(rotate1.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv2 = new KeyValue(rotate1.angleProperty(), -720, Interpolator.EASE_BOTH);

        Rotate rotate2 = new Rotate(0, 300, 300);
        arc1.getTransforms().add(rotate2);
        KeyValue kv3 = new KeyValue(rotate2.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv4 = new KeyValue(rotate2.angleProperty(), -720, Interpolator.EASE_BOTH);

        Rotate rotate3 = new Rotate(0, 300, 300);
        arc2.getTransforms().add(rotate3);
        KeyValue kv5 = new KeyValue(rotate3.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv6 = new KeyValue(rotate3.angleProperty(), -720, Interpolator.EASE_BOTH);

        Rotate rotate4 = new Rotate(0, 300, 300);
        arc3.getTransforms().add(rotate4);
        KeyValue kv7 = new KeyValue(rotate4.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv8 = new KeyValue(rotate4.angleProperty(), -720, Interpolator.EASE_BOTH);

        Rotate rotate5 = new Rotate(0, 300, 300);
        arc4.getTransforms().add(rotate5);
        KeyValue kv9 = new KeyValue(rotate5.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv10 = new KeyValue(rotate5.angleProperty(), -720, Interpolator.EASE_BOTH);

        Rotate rotate6 = new Rotate(0, 300, 300);
        arc5.getTransforms().add(rotate6);
        KeyValue kv11 = new KeyValue(rotate6.angleProperty(), 0, Interpolator.EASE_BOTH);
        KeyValue kv12 = new KeyValue(rotate6.angleProperty(), -720, Interpolator.EASE_BOTH);

        KeyValue kv13 = new KeyValue(rotate1.angleProperty(), -720, Interpolator.EASE_BOTH);
        KeyValue kv14 = new KeyValue(rotate2.angleProperty(), -720, Interpolator.EASE_BOTH);
        KeyValue kv15 = new KeyValue(rotate3.angleProperty(), -720, Interpolator.EASE_BOTH);
        KeyValue kv16 = new KeyValue(rotate4.angleProperty(), -720, Interpolator.EASE_BOTH);
        KeyValue kv17 = new KeyValue(rotate5.angleProperty(), -720, Interpolator.EASE_BOTH);
        KeyValue kv18 = new KeyValue(rotate6.angleProperty(), -720, Interpolator.EASE_BOTH);

        //0度停留0.2秒
        KeyFrame kf = new KeyFrame(Duration.millis(200), kv1, kv3, kv5,
                kv7, kv9, kv11);
        //10秒转到-720度
        KeyFrame kf1 = new KeyFrame(Duration.seconds(10), kv2, kv4, kv6,
                kv8, kv10, kv12);
        //在-720度停留0.2秒
        KeyFrame kf2 = new KeyFrame(Duration.seconds(10.2), kv13, kv14, kv15,
                kv16, kv17, kv18);
        timeline.getKeyFrames().addAll(kf, kf1, kf2);

        //反方向旋转
        timeline.setAutoReverse(true);

        //无限旋转
        timeline.setCycleCount(-1);
        root.getChildren().addAll(arc, arc1, arc2, arc3, arc4, arc5);

    }

    private void four(AnchorPane root, Timeline timeline) {
        int radiusXY = 217;
        Arc arc3 = newArc(300, 300, radiusXY, radiusXY, 105, 0);
        arc3.setStrokeLineCap(StrokeLineCap.ROUND);
        arc3.getStrokeDashArray().addAll(260.0, 10.0, 0.0, 10.0, 0.0, 10.0);
        arc3.setStrokeWidth(5);

        Arc arc4 = newArc(300, 300, radiusXY, radiusXY, 100, 122);
        arc4.setStrokeLineCap(StrokeLineCap.ROUND);
        arc4.getStrokeDashArray().addAll(250.0, 10.0, 0.0, 10.0, 0.0, 10.0);
        arc4.setStrokeWidth(5);

        Arc arc5 = newArc(300, 300, radiusXY, radiusXY, 100, 240);
        arc5.setStrokeLineCap(StrokeLineCap.ROUND);
        arc5.getStrokeDashArray().addAll(250.0, 10.0, 0.0, 10.0, 0.0, 10.0);
        arc5.setStrokeWidth(5);
        // arc3的旋转补帧器
        Rotate rotate = new Rotate(0, 300, 300);
        arc3.getTransforms().add(rotate);
        //旋转0度
        KeyValue kv = new KeyValue(rotate.angleProperty(), 0);
        //旋转一圈
        KeyValue kv1 = new KeyValue(rotate.angleProperty(), 360);

        Rotate rotate1 = new Rotate(0, 300, 300);
        arc4.getTransforms().add(rotate1);

        KeyValue kv2 = new KeyValue(rotate1.angleProperty(), 0);
        KeyValue kv3 = new KeyValue(rotate1.angleProperty(), 360);

        Rotate rotate2 = new Rotate(0, 300, 300);
        arc5.getTransforms().add(rotate2);

        KeyValue kv4 = new KeyValue(rotate2.angleProperty(), 0);
        KeyValue kv5 = new KeyValue(rotate2.angleProperty(), 360);

        KeyFrame kf = new KeyFrame(Duration.seconds(12), kv, kv1, kv2, kv3, kv4, kv5);


        timeline.getKeyFrames().add(kf);
        //反方向旋转
        timeline.setAutoReverse(true);
        //无限旋转
        timeline.setCycleCount(-1);


        root.getChildren().addAll(arc3, arc4, arc5);

    }

    private void five(AnchorPane root) {
        int radiusXY = 200;
        Arc arc4 = newArc(300, 300, radiusXY, radiusXY, 260, 40);
        arc4.setStrokeLineCap(StrokeLineCap.BUTT);
        arc4.setStrokeWidth(15);

        Arc arc5 = newArc(300, 300, radiusXY, radiusXY, 95, 302);
        arc5.setStrokeLineCap(StrokeLineCap.ROUND);
        arc5.getStrokeDashArray().addAll(0.0, 10.0);
        arc5.setStrokeWidth(6);
        root.getChildren().addAll(arc4, arc5);
    }

    private void six(AnchorPane root) {
        int radiusXY = 178;
        Arc arc1 = newArc(300, 300, radiusXY, radiusXY, 360, 0);
        arc1.setStrokeLineCap(StrokeLineCap.BUTT);
        arc1.setStrokeWidth(2);

        Arc arc2 = newArc(300, 300, radiusXY, radiusXY, 360, 0);
        arc2.setStrokeLineCap(StrokeLineCap.ROUND);
        arc2.setStrokeWidth(2);

        Arc arc3 = new Arc();
        arc3.setCenterX(300);
        arc3.setCenterY(300);
        arc3.setRadiusX(163);
        arc3.setRadiusY(163);
        arc3.setStrokeLineCap(StrokeLineCap.ROUND);
        arc3.setLength(360);
        arc3.setStartAngle(0);
        arc3.setStrokeWidth(3);
        arc3.setFill(Paint.valueOf("#66B3FF"));
        arc3.setType(ArcType.CHORD);
        arc3.setOpacity(0.7);

        root.getChildren().addAll(arc1, arc2, arc3);
    }

    private void seven(AnchorPane root) {
        Path path = new Path();
        MoveTo mv = new MoveTo(215, 153);
        LineTo line = new LineTo(155.0, 50.0);
        QuadCurveTo qt = new QuadCurveTo(70, -45, 160, -42);
        qt.setAbsolute(false);

        LineTo line1 = new LineTo(315, 130.0);
        QuadCurveTo qt1 = new QuadCurveTo(260, 125, 215, 153);

        path.getElements().addAll(mv, line, qt, line1, qt1);
        path.setStrokeLineCap(StrokeLineCap.ROUND);
        path.setStrokeWidth(2);
        path.setSmooth(true);
        path.setStroke(Paint.valueOf("#1E90FF"));
        path.setOpacity(0.7);
        root.getChildren().addAll(path);

        surroundPlanet(anchorPane);
    }

    private void surroundPlanet(AnchorPane root) {
        Arc favoriteType_ring = newArcOfStarRing(250, 80, 45, 45, "favoriteType_ring");
        ImageView favoriteType = newSurroundPlanetImages("/fxml/css/images/setep/FavoriteType_No.png", 210, 40, "favoriteType");

        Arc frequency_ring = newArcOfStarRing(490, 180, 45, 45, "frequency_ring");
        ImageView frequency = newSurroundPlanetImages("/fxml/css/images/setep/Frequency_No.png", 450, 140, "frequency");

        Arc number_ring = newArcOfStarRing(470, 445, 45, 45, "number_ring");
        ImageView number = newSurroundPlanetImages("/fxml/css/images/setep/number_No.png", 430, 405, "number");

        Arc screeSaver_ring = newArcOfStarRing(180, 480, 45, 45, "screeSaver_ring");
        ImageView screeSaver = newSurroundPlanetImages("/fxml/css/images/setep/ScreenSaver_No.png", 140, 440, "screeSaver");

        Arc updateTime_ring = newArcOfStarRing(70, 270, 45, 45, "updateTime_ring");
        ImageView updateTime = newSurroundPlanetImages("/fxml/css/images/setep/UpdateTime_No.png", 30, 230, "updateTime");

        root.getChildren().addAll(favoriteType_ring, favoriteType, frequency_ring, frequency, number_ring, number,
                screeSaver_ring, screeSaver, updateTime_ring, updateTime);

        Rotate rotate = new Rotate(0, 300, 300);
        favoriteType.getTransforms().add(rotate);
        KeyValue kv1 = new KeyValue(rotate.angleProperty(), 0);
        KeyValue kv2 = new KeyValue(rotate.angleProperty(), 360);

        Rotate rotate1 = new Rotate(0, 300, 300);
        frequency.getTransforms().add(rotate);
        KeyValue kv3 = new KeyValue(rotate1.angleProperty(), 0);
        KeyValue kv4 = new KeyValue(rotate1.angleProperty(), 360);


        Rotate rotate2 = new Rotate(0, 300, 300);
        number.getTransforms().add(rotate);
        KeyValue kv5 = new KeyValue(rotate2.angleProperty(), 0);
        KeyValue kv6 = new KeyValue(rotate2.angleProperty(), 360);


        Rotate rotate3 = new Rotate(0, 300, 300);
        screeSaver.getTransforms().add(rotate);
        KeyValue kv7 = new KeyValue(rotate3.angleProperty(), 0);
        KeyValue kv8 = new KeyValue(rotate3.angleProperty(), 360);

        Rotate rotate4 = new Rotate(0, 300, 300);
        updateTime.getTransforms().add(rotate);
        KeyValue kv9 = new KeyValue(rotate4.angleProperty(), 0);
        KeyValue kv10 = new KeyValue(rotate4.angleProperty(), 360);


        KeyFrame kf = new KeyFrame(Duration.seconds(50), kv1, kv2, kv3, kv4, kv5, kv6, kv7, kv8, kv9, kv10);
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().add(kf);
        timeline.setCycleCount(-1);
        timeline.play();


        menusMouseClicked();
    }

    private Arc newArcOfStarRing(double centerX, double centerY,
                                 double radiusX, double radiusY, String id) {
        Arc arc = new Arc(centerX, centerY, radiusX, radiusY, 0, 360);
        arc.setFill(Paint.valueOf("#ffffff00"));
        arc.setSmooth(true);
        arc.setStroke(Paint.valueOf("#5cb3cc"));
        arc.setType(ArcType.OPEN);
        arc.setStrokeLineCap(StrokeLineCap.BUTT);
        arc.setStrokeWidth(5);
        arc.setId(id);
        arc.setOpacity(0);
        return arc;
    }

    /**
     * 为旋转菜单添加点击时间
     */
    private void menusMouseClicked() {
        ImageView favoriteType = ((ImageView) $("favoriteType"));
        favoriteType.setOnMouseClicked(event -> {
            Bounds bounds = favoriteType.localToParent(anchorPane.getBoundsInLocal());
            System.out.println(bounds.getCenterY());
            System.out.println(bounds.getCenterX());
//            mouseClickedForTranslate(favoriteType);
        });
        ImageView frequency = ((ImageView) $("frequency"));
        frequency.setOnMouseClicked(event -> {
            System.out.println(favoriteType.getScaleY());
            System.out.println(favoriteType.getScaleX());
            mouseClickedForTranslate(frequency);
        });
        ImageView number = ((ImageView) $("number"));
        number.setOnMouseClicked(event -> {
            System.out.println(favoriteType.getLayoutX());
            System.out.println(favoriteType.getLayoutY());
            mouseClickedForTranslate(number);
        });
        ImageView screeSaver = ((ImageView) $("screeSaver"));
        screeSaver.setOnMouseClicked(event ->
                mouseClickedForTranslate(screeSaver)
        );
        ImageView updateTime = ((ImageView) $("updateTime"));
        updateTime.setOnMouseClicked(event ->
                mouseClickedForTranslate(updateTime)
        );
    }

    /**
     * 旋转菜单的点击事件
     *
     * @param imageView 要平移的菜单图片
     */
    private void mouseClickedForTranslate(ImageView imageView) {
        //平移动画
        KeyValue kv11 = new KeyValue(imageView.translateXProperty(), imageView.getTranslateX() - 100, Interpolator.EASE_BOTH);
        KeyValue kv12 = new KeyValue(imageView.translateYProperty(), imageView.getTranslateY() - 100, Interpolator.EASE_BOTH);
        KeyFrame kf2 = new KeyFrame(Duration.seconds(2), kv11, kv12);
        Timeline timeline1 = new Timeline();
        timeline1.getKeyFrames().add(kf2);
        timeline1.play();
    }

    /**
     * new 环绕菜单images
     *
     * @param url
     * @param x
     * @param y
     * @param id
     * @return
     */
    private ImageView newSurroundPlanetImages(String url, int x, int y, String id) {
        ImageView imageView = new ImageView(new Image(url));
        imageView.setFitHeight(80);
        imageView.setFitWidth(80);
        imageView.setX(x);
        imageView.setY(y);
        imageView.setId(id);
        imageView.setOpacity(0.8);
        return imageView;
    }




    private void smilingFace(AnchorPane root) {
        Arc arc1 = newArc(260, 270, 28, 28, 160, 10);
        arc1.setStrokeLineCap(StrokeLineCap.ROUND);
        arc1.setStroke(Paint.valueOf("#F8F8FF"));
        arc1.setStrokeWidth(7);

        Arc arc2 = newArc(345, 270, 28, 28, 160, 10);
        arc2.setStrokeLineCap(StrokeLineCap.ROUND);
        arc2.setStroke(Paint.valueOf("#F8F8FF"));
        arc2.setStrokeWidth(7);

        Arc arc3 = newArc(300, 310, 60, 60, 125, 210);
        arc3.setStrokeLineCap(StrokeLineCap.ROUND);
        arc3.setStroke(Paint.valueOf("#F8F8FF"));
        arc3.setStrokeWidth(5);
        root.getChildren().addAll(arc1, arc2, arc3);
    }

    /**
     * @param root
     */
    private void badFace(AnchorPane root) {
        Arc arc1 = newArc(250, 290, 38, 38, 110, 40);
        arc1.setStrokeLineCap(StrokeLineCap.ROUND);
        arc1.setStroke(Paint.valueOf("#F8F8FF"));
        arc1.setStrokeWidth(7);

        Line line = new Line(320, 270, 370, 270);
        line.setFill(Paint.valueOf("#F8F8FF"));
        line.setStroke(Paint.valueOf("#F8F8FF"));
        line.setStrokeLineCap(StrokeLineCap.ROUND);
        line.setStrokeWidth(7);

        Arc arc3 = newArc(300, 310, 60, 60, 125, 210);
        arc3.setStrokeLineCap(StrokeLineCap.ROUND);
        arc3.setStroke(Paint.valueOf("#F8F8FF"));
        arc3.setStrokeWidth(5);
        root.getChildren().addAll(arc1, line, arc3);
    }

    private void weirdFace(AnchorPane root) {
        Arc arc1 = new Arc(260, 270, 38, 38, 160, 10);
//        arc1.setStrokeLineCap(StrokeLineCap.ROUND);
        arc1.setType(ArcType.OPEN);
        arc1.setStroke(Paint.valueOf("#F8F8FF"));
        arc1.setStrokeWidth(7);

        Arc arc2 = newArc(345, 270, 28, 28, 160, 10);
        arc2.setStrokeLineCap(StrokeLineCap.ROUND);
        arc2.setStroke(Paint.valueOf("#F8F8FF"));
        arc2.setStrokeWidth(7);
        Arc arc3 = newArc(300, 310, 60, 60, 125, 210);
        arc3.setStrokeLineCap(StrokeLineCap.ROUND);
        arc3.setStroke(Paint.valueOf("#F8F8FF"));
        arc3.getStrokeDashArray().addAll(120.0, 0.0, 10.0, 0.0);
        arc3.setStrokeWidth(5);
        root.getChildren().addAll(arc1, arc2, arc3);
    }

    private Arc newArc(double centerX, double centerY,
                       double radiusX, double radiusY,
                       double length, double startAngle) {
        Arc arc = new Arc(centerX, centerY, radiusX, radiusY, startAngle, length);
        arc.setFill(Paint.valueOf("#ffffff00"));
        arc.setSmooth(true);
        arc.setStroke(Paint.valueOf("#1E90FF"));
        arc.setType(ArcType.OPEN);
        return arc;
    }

    /**
     * 选择界面元素
     *
     * @param id
     * @return
     */
    public Object $(String id) {
        return anchorPane.lookup("#" + id);
    }

}
